<template>
  <p-drawer
    :title="title"
    :width="width"
    placement="right"
    :closable="false"
    @close="close"
    :visible="visible">
  
    <p-spin :spinning="confirmLoading">
      <p-form :form="form">

        <p-form-item label="产品/物料小类编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <f-popup
            contextPath="basic-business"
            v-decorator="['mscode', validatorRules.mscode]"
            :trigger-change="true"
            org-fields="mid,mcode,mname"
            dest-fields="msid,mscode,msname"
            code="mes_cpinfo_select"
            @callback="popupCallback"/>
        </p-form-item>
        <p-form-item label="产品/物料小类名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'msname', validatorRules.msname]" placeholder="请输入产品/物料小类名称"></p-input>
        </p-form-item>
        <p-form-item label="检验类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['inspectType', validatorRules.inspectType]" :trigger-change="true" dictCode="inspect_type" placeholder="请选择检验类型"/>
        </p-form-item>
        <p-form-item label="检验属性" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="radio" v-decorator="['inspectAttribute', validatorRules.inspectAttribute]" :trigger-change="true" dictCode="inspect_attribute" placeholder="请选择检验属性"/>
        </p-form-item>
        <p-form-item label="抽样方式" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="radio" v-decorator="['simplingMethod', validatorRules.simplingMethod]" :trigger-change="true" dictCode="rc_inspect_method" placeholder="请选择抽样方式"/>
        </p-form-item>
        <p-form-item label="正常抽样数量" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'normalSimplingQty', validatorRules.normalSimplingQty]" placeholder="请输入正常抽样数量"></p-input>
        </p-form-item>
        <p-form-item label="加严系数" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'tightCoefficient', validatorRules.tightCoefficient]" placeholder="请输入加严系数"></p-input>
        </p-form-item>
        <p-form-item label="是否动态调整" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="radio" v-decorator="['isDynamicRegulation', validatorRules.isDynamicRegulation]" :trigger-change="true" dictCode="ba_yn" placeholder="请选择是否动态调整"/>
        </p-form-item>
        <p-form-item label="正常（N1）" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input-number v-decorator="[ 'normalN1', validatorRules.normalN1]" placeholder="请输入正常（N1）" style="width: 100%"/>
        </p-form-item>
        <p-form-item label="直通（N2）" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input-number v-decorator="[ 'passThroughN2', validatorRules.passThroughN2]" placeholder="请输入直通（N2）" style="width: 100%"/>
        </p-form-item>
        <p-form-item label="是否需要供应商出场记录" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="radio" v-decorator="['isNeedCustoutrc', validatorRules.isNeedCustoutrc]" :trigger-change="true" dictCode="ba_yn" placeholder="请选择是否需要供应商出场记录"/>
        </p-form-item>
        <p-form-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="radio" v-decorator="['status', validatorRules.status]" :trigger-change="true" dictCode="mes_status" placeholder="请选择状态"/>
        </p-form-item>

      </p-form>
    </p-spin>
    <p-button type="primary" @click="handleOk">确定</p-button>
    <p-button type="primary" @click="handleCancel">取消</p-button>
  </p-drawer>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { SubmitOnchangMixin } from '@/mixins/SubmitOnchangMixin'
  import { validateDuplicateValue } from '@/utils/util'
  import JDictSelectTag from "@/components/dict/JDictSelectTag"
  
  export default {
    name: "MesQcInspectRuleBytypeModal",
	mixins: [SubmitOnchangMixin],
    components: { 
      JDictSelectTag,
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title: '操作',
        width: 800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          mscode: {rules: [
            {required: true, message: '请输入产品/物料小类编码!'},
          ]},
          msname: {rules: [
            {required: true, message: '请输入产品/物料小类名称!'},
          ]},
          inspectType: {rules: [
            {required: true, message: '请输入检验类型!'},
          ]},
          inspectAttribute: {rules: [
            {required: true, message: '请输入检验属性!'},
          ]},
          simplingMethod: {rules: [
            {required: true, message: '请输入抽样方式!'},
          ]},
          normalSimplingQty: {rules: [
            {required: true, message: '请输入正常抽样数量!'},
          ]},
          tightCoefficient: {rules: [
            {required: true, message: '请输入加严系数!'},
           {pattern:/^-?\d+\.?\d*$/, message: '请输入数字!'},
          ]},
          isDynamicRegulation: {rules: [
            {required: true, message: '请输入是否动态调整!'},
          ]},
          normalN1: {rules: [
           {pattern:/^-?\d+\.?\d*$/, message: '请输入数字!'},
          ]},
          passThroughN2: {rules: [
           {pattern:/^-?\d+\.?\d*$/, message: '请输入数字!'},
          ]},
          isNeedCustoutrc: {rules: [
            {required: true, message: '请输入是否需要供应商出场记录!'},
          ]},
          status: {rules: [
            {required: true, message: '请输入状态!'},
          ]},
        },
        url: {
          add: '/mes-quality/mesQcInspectRuleBytype/add',
          edit: '/mes-quality/mesQcInspectRuleBytype/edit',
        }
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },

      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'mscode','msname','inspectType','inspectAttribute','simplingMethod','normalSimplingQty','tightCoefficient','isDynamicRegulation','dynamicAdjustment','normalN1','passThroughN2','isNeedCustoutrc','status','createBy','createTime','remark'))
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
              that.confirmLoading = true;
              let httpurl = '';
              let method = '';
              let formData = Object.assign(this.model, values);
              this.formData = Object.assign(this.model, values);
              //新增触发
              if(!this.model.id){
                httpurl+=this.url.add;
                method = 'post';
                if(this.EnhanceJS.afterAdd){
                  //新增之前
                  let Obj = eval(this.EnhanceJS.afterAdd)
                  new Obj(this)
                }else{
                  console.log("表单提交数据",formData)
                  that.modalFrom(httpurl,formData,method)
                }
              }else{
                //编辑触发
                httpurl+=this.url.edit;
                method = 'put';
                if(this.EnhanceJS.afterEdit){
                 //编辑之前
                  let Obj = eval(this.EnhanceJS.afterEdit)
                  new Obj(this)
                }else{
                  console.log("表单提交数据",formData)
                  that.modalFrom(httpurl,formData,method)
                }
              }
          }

        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'mscode','msname','inspectType','inspectAttribute','simplingMethod','normalSimplingQty','tightCoefficient','isDynamicRegulation','dynamicAdjustment','normalN1','passThroughN2','isNeedCustoutrc','status','createBy','createTime','remark'))
      }
      
    }
  }
</script>

<style lang="less" scoped>
/** Button按钮间距 */
  .ant-btn {
    margin-left: 30px;
    margin-bottom: 30px;
    float: right;
  }
</style>
